﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Task.Common.Interface;
using Common.Service;
using System.Collections;
using Task.Common.Entity;
using Common.BasePages;
using Ultimus.WFServer;
using OC.Common.Entity;
using UltLibrary.UltInfo;
using System.Xml;

namespace Task
{
    public partial class OpenForm : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            string taskid = Request.QueryString["TaskId"];
            string processName = Request.QueryString["ProcessName"];
            string stepName = Request.QueryString["StepName"];

        }
        public void RedirectPage(string taskId, string processName, string stepName)
        {
            string strProcessName = processName;
            string strStepName = stepName;
            string strIncidentNo = "";
            string strUserName = "";

            if (!string.IsNullOrEmpty(taskId))
            {
                Ultimus.WFServer.Task task = new Ultimus.WFServer.Task();
                task.InitializeFromTaskId(taskId);
                strUserName = ((UserEntity)UserInfo).AD_ACCOUNT;
                strProcessName = task.strProcessName;
                strStepName = task.strStepName;
                strIncidentNo = task.nIncidentNo.ToString();
            }
            StepSetting stepSetting = null;
            foreach (StepSetting ss in GetStepSettings())
            {
                if (ss.StepName == strStepName && ss.Process == strProcessName)
                {
                    stepSetting = ss;
                    break;
                }
            }
            if (string.IsNullOrEmpty(strIncidentNo))
            {
                strIncidentNo = "0";
            }
            string URL = "";

            if (stepSetting != null)
            {
                if (stepSetting.IsAspx == "1")
                {
                    URL = stepSetting.PageName + "?ProcessName=" + strProcessName + "&StepName=" + strStepName + "&Incident=" + strIncidentNo + "&UserName=" + strUserName;
                }
                
            }

            if (URL == "")
            {
                throw new Exception("Process page not found!ProcessName:" + strProcessName + ",StepName:" + strStepName + ",RecordCount:" + GetStepSettings().Count.ToString());
            }

            HttpContext.Current.Response.Redirect(URL);
        }

        public static ArrayList GetStepSettings()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Request.MapPath("~/StepSettings.xml"));

            ArrayList al = new ArrayList();
            foreach (XmlNode xn in doc.DocumentElement.ChildNodes)
            {
                StepSetting ss = new StepSetting();
                foreach (XmlAttribute xa in xn.Attributes)
                {
                    if (xa.Name == "Process")
                        ss.Process = xa.Value;
                    if (xa.Name == "StepName")
                        ss.StepName = xa.Value;
                    else if (xa.Name == "PageName")
                        ss.PageName = xa.Value;
                    else if (xa.Name == "IsAspx")
                        ss.IsAspx = xa.Value;
                }
                al.Add(ss);
            }
            return al;
        }

    }
}